package com.controller.manage;

import com.common.paging.PagingData;
import com.dto.excution.ListExcution;
import com.entity.StudentInfo;
import com.entity.Teacherinfo;
import com.enums.ListEnum;
import com.service.ManageService;
import com.service.OrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/2/13 0013.
 */
@RequestMapping("manage")
@RestController
public class ManageController {
    @Autowired
    private ManageService service;
    @Autowired
    private OrderService orderService;

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @ApiOperation(value = "后台固定登陆")
    @PostMapping(value = "/backstageLogin")
    @ResponseBody
    public ListExcution<Void> backstageLogin(String userName, String password) {
        if (userName.equals("lingyingjiajiao") && password.equals("lingyingjiajiao")) {
            return new ListExcution<Void>(null, ListEnum.SUCCESS);
        } else {
            return new ListExcution<Void>(null, ListEnum.FAIL);
        }
    }

    @ApiOperation(value = "后台/查询所有投诉建议")
    @PostMapping(value = "/findAdvicesList")
    @ResponseBody
    public PagingData findAdvicesList(Integer page, Integer length) {
        page = (page - 1) * 10;
        PagingData list = service.findAdvicesList(page, length);
        return list;
    }

    @ApiOperation(value = "后台/删除投诉建议")
    @PostMapping(value = "/setAdvices")
    @ResponseBody
    public ListExcution<Void> setAdvices(Integer id) {
        int success = service.setAdvices(id);
        if (success > 0) {
            return new ListExcution<Void>(null, ListEnum.SUCCESS);
        } else {
            return new ListExcution<Void>(null, ListEnum.FAIL);
        }
    }

    @ApiOperation(value = "查询所有老师的所有信息")
    @PostMapping(value = "/findAll")
    @ResponseBody
    public Map findAll(Integer page, Integer length, String search) {
        if (search.equals("显示")) {
            search = "YES";
        } else if (search.equals("隐藏")) {
            search = "NO";
        }
        Map map = new HashMap();
        List<Teacherinfo> pagingData = service.findTeacherList(page, length, search);
        int count = service.findCount(search);
        map.put("count", count);
        map.put("data", pagingData);
        return map;
    }

    @ApiOperation(value = "查询UserType的List")
    @PostMapping(value = "/findUserTypeList")
    @ResponseBody
    public PagingData findUserTypeList(Integer page, Integer length, String search) {
        PagingData pagingData = service.findUserTypeList(page, length, search);
        return pagingData;
    }

    @ApiOperation(value = "后台/查我的发布")
    @PostMapping(value = "/houduanMyRelease")
    @ResponseBody
    public ListExcution<List<StudentInfo>> houduanMyRelease(Integer userId) {
        List<StudentInfo> studentInfo = service.houduanMyRelease(userId);
        return new ListExcution<List<StudentInfo>>(studentInfo, ListEnum.SUCCESS);
    }

    @ApiOperation(value = "后台/查所有认证信息")
    @PostMapping(value = "/findCertificationList")
    @ResponseBody
    public PagingData findCertificationList(Integer page, Integer length, String search, Integer type) {
        page = (page - 1) * 10;
        PagingData list = service.findCertificationList(page, length, search, type);
        return list;
    }

    @ApiOperation(value = "修改认证状态")
    @PostMapping(value = "/setCertification")
    @ResponseBody
    public ListExcution<Void> setCertification(Integer id, Integer type,Integer pid) {
        int update = service.setCertification(id,type,pid);
        if (update > 0) {
            return new ListExcution<Void>(null, ListEnum.SUCCESS);
        } else {
            return new ListExcution<Void>(null, ListEnum.FAIL);
        }
    }

    @ApiOperation(value = "认证短信")
    @PostMapping(value = "/identifyMsg")
    @ResponseBody
    public String identifyMsg(String phone, String type,int types) {
        String url = "https://api.mysubmail.com/message/xsend";
        Map<String, String> map = new HashMap<String, String>();
        map.put("appid", "28633");
        map.put("to", phone);
        if (type.equals("pass")){
            if(types == 1){
                map.put("project", "I4wWT4");
            }else {
                map.put("project", "YDcnf");
            }
        }else{
            map.put("project", "fuBFD2");
        }
        map.put("signature", "a8f3a7b6a8c8c3af115a1fc84c3fd9b9");
        ResponseEntity<String> responseEntity =
                restTemplate().postForEntity(url, map, String.class);
        System.out.println("identify" + responseEntity.getBody());
        return responseEntity.getBody();
    }

    @ApiOperation(value = "后台/查询用户个人流水")
    @PostMapping(value = "/findUserBillList")
    @ResponseBody
    public PagingData findUserBillList(Integer page, Integer length, String search) {
        page = (page - 1) * 10;
        PagingData list = service.findUserBillList(page, length, search);
        return list;
    }

    @ApiOperation(value = "后台/查询名师值列表")
    @PostMapping(value = "/findTeacherValueList")
    @ResponseBody
    public PagingData findTeacherValueList(Integer page, Integer length, String search) {
        page = (page - 1) * 10;
        PagingData list = service.findTeacherValueList(page, length, search);
        return list;
    }

    @ApiOperation(value = "后台/修改名师值")
    @PostMapping(value = "/setTeacherValue")
    @ResponseBody
    public ListExcution<Void> setTeacherValue(Integer value, Integer id) {
        int count = service.setTeacherValue(value,id);
        if(count > 0){
            return new ListExcution<Void>(null,ListEnum.SUCCESS);
        }else{
            return new ListExcution<Void>(null,ListEnum.FAIL);
        }
    }

    @ApiOperation(value = "后台/修改名师值")
    @PostMapping(value = "/findRewardList")
    @ResponseBody
    public PagingData findRewardList(Integer page, Integer length, String search) {
        page = (page - 1) * 10;
        PagingData data = service.findRewardList(page,length,search);
        return data;
    }

    @ApiOperation(value = "后台/查推广排行")
    @PostMapping(value = "/findShareList")
    @ResponseBody
    public PagingData findShareList(Integer page, Integer length, String search) {
        page = (page - 1) * 10;
        PagingData data = service.findShareList(page,length,search);
        return data;
    }

    @ApiOperation(value = "查询用户类别")
    @PostMapping(value = "/findUserType")
    @ResponseBody
    public Map findUserType() {
        Map map = service.findUserType();
        return map;
    }

    @ApiOperation(value = "后台/通过订单编号查中途退款")
    @PostMapping(value = "/findMidwayRefund")
    @ResponseBody
    public PagingData findMidwayRefund(Integer page, Integer length, String search) {
        page = (page - 1) * 10;
        PagingData data = service.findMidwayRefund(page,length,search);
        return data;
    }

    @ApiOperation(value = "后台/查退款列表")
    @PostMapping(value = "/findRefundRecordList")
    @ResponseBody
    public PagingData findRefundRecordList(Integer page, Integer length, String search) {
        page = (page - 1) * 10;
        PagingData data = service.findRefundRecordList(page,length,search);
        return data;
    }

    @ApiOperation(value = "后台/删除退款订单")
    @PostMapping(value = "/deleteRefundRecord")
    @ResponseBody
    public ListExcution deleteRefundRecord(Integer id,String phone) {
        int count = service.deleteRefundRecord(id);
        if(count > 0){
            String url = "https://api.mysubmail.com/message/xsend";
            Map<String, String> map = new HashMap<String, String>();
            map.put("appid", "28633");
            map.put("to", phone);
            map.put("project", "1fF1w3");
            map.put("signature", "a8f3a7b6a8c8c3af115a1fc84c3fd9b9");
            ResponseEntity<String> responseEntity =
                    restTemplate().postForEntity(url, map, String.class);
            return new ListExcution<Void>(null,ListEnum.SUCCESS);
        }else{
            return new ListExcution<Void>(null,ListEnum.FAIL);
        }
    }

    @ApiOperation(value = "后台/退款/取消订单")
    @PostMapping(value = "/deleteOrder")
    @ResponseBody
    public ListExcution deleteOrder(Integer id,String phone) {
        int count = orderService.deleteOrder(id);
        if(count > 0){
            String url = "https://api.mysubmail.com/message/xsend";
            Map<String, String> map = new HashMap<String, String>();
            map.put("appid", "28633");
            map.put("to", phone);
            map.put("project", "1fF1w3");
            map.put("signature", "a8f3a7b6a8c8c3af115a1fc84c3fd9b9");
            ResponseEntity<String> responseEntity =
                    restTemplate().postForEntity(url, map, String.class);
            return new ListExcution<Void>(null,ListEnum.SUCCESS);
        }else{
            return new ListExcution<Void>(null,ListEnum.FAIL);
        }
    }

    @ApiOperation(value = "后台/查询订单成交列表")
    @PostMapping(value = "/findTransactionList")
    @ResponseBody
    public PagingData findTransactionList(Integer page, Integer length, String search) {
        page = (page - 1) * 10;
        PagingData data = service.findTransactionList(page,length,search);
        return data;
    }

    @ApiOperation(value = "后台/查询需要提现的老师列表")
    @PostMapping(value = "/findTeacherMoneyList")
    @ResponseBody
    public PagingData findTeacherMoneyList(Integer page, Integer length, String search) {
        page = (page - 1) * 10;
        PagingData data = service.findTeacherMoneyList(page,length,search);
        return data;
    }

    @ApiOperation(value = "后台/查询需要提现的老师列表")
    @PostMapping(value = "/resetTeacherMoney")
    @ResponseBody
    public ListExcution<Void> resetTeacherMoney(Integer userId, BigDecimal lyMoney) {
        int success = service.resetTeacherMoney(userId,lyMoney);
        if(success == -1){
            return new ListExcution<Void>(null,ListEnum.FAILDATA);
        }else if(success == 0){
            return new ListExcution<Void>(null,ListEnum.FAIL);
        }else{
            return new ListExcution<Void>(null,ListEnum.SUCCESS);
        }
    }

    @ApiOperation(value = "后台/折线图查订单成交记录")
    @PostMapping(value = "/findStatistics")
    @ResponseBody
    public ListExcution<List> findStatistics() {
        List list = service.findStatistics();
       return new ListExcution<List>(list,ListEnum.SUCCESS);
    }

    @ApiOperation(value = "后台/漏单补单")
    @PostMapping(value = "/missOrder")
    @ResponseBody
    public ListExcution<Void> missOrder(String studentPhone,String teacherPhone,String transactionId,String outTradeNo,Integer classTime,BigDecimal totalPrice,BigDecimal price) {
        int count = service.missOrder(studentPhone,teacherPhone,transactionId,outTradeNo,classTime,totalPrice,price);
        if(count > 0){
            return new ListExcution<Void>(null,ListEnum.SUCCESS);
        }else{
            return new ListExcution<Void>(null,ListEnum.SUCCESS);
        }
    }

}

